package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.excel.model.StudentModel;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Override
    public PageResult<Student> pageQuery(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> pageInfo = studentMapper.pageQuery(studentQueryParam);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    /**
     * 批量删除学生信息
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteById(List<Integer> ids) {
        //1. 根据ID批量删除学员基本信息
        studentMapper.deleteByIds(ids);
    }

    /**
     * 新增学员信息
     *
     * @param student
     */
    @Override
    public void save(Student student) {
        //补全信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);

    }

    /**
     * 根据ID查询学员信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getById(id);
    }

    /**
     * 修改学员信息
     *
     * @param student
     */
    @Override
    public void update(Student student) {
        //补全基础属性
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }

    /**
     * 学员违纪处理
     *
     * @param id
     * @param score
     */
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }

    @Override
    @Transactional
    public void importStudent(List<StudentModel> stuList, Integer clazzId) {
        /*for (StudentModel studentModel : stuList) {
            Student student = studentModel.toStudent(clazzId);
            studentMapper.insert(student);
        }*/
        //批量插入
        List<Student> studentList = stuList.stream().map(sm -> sm.toStudent(clazzId)).toList();
        studentMapper.insertBatch(studentList);
    }

    @Override
    public List<StudentModel> list(StudentQueryParam queryParam) {
        //查询数据库中的学员信息
        List<Student> students = studentMapper.pageQuery(queryParam);
        //把学员信息转成StudentModel对象
        List<StudentModel> studentModelList = students.stream().map(StudentModel::convert).collect(Collectors.toList());
        return studentModelList;
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }
}